<?php

/**
 *	Common methods to every database type (mysql, pgsql...)
 *	
 */
abstract class Cms_Adapter_Abstract extends Zend_Db_Table
{
	protected $_name;
	
	protected $_schema;
	
	protected $_database;
	
	public function setTable($table)
	{
		$this->_name = $table;
		return $this;
	}
	
	public function getTable() {
		return $this->_name;
	}
	
	public function setDatabase($database = null)
	{
		if(!empty($database)) {
			$this->_database = $database;
		}
		return $this;
	}
	
	public function getDatabase() {
		return $this->_database;
	}
	
	public function setSchema($schema = null)
	{
		if(empty($schema))
		{
			// Default adapter
			$db = Zend_Db_Table::getDefaultAdapter();

			switch(get_class($db))
			{
				case 'Zend_Db_Adapter_Pdo_Pgsql':
					$schema = 'public';
					break;
			}
		}
		
		$this->_schema = $schema;
		return $this;
	}
	
	public function getSchema()
	{
		if($this->_schema === null) {
			$this->setSchema();
		}
		return $this->_schema;
	}
	
	public function rename($value, $primary = null)
	{
		$name = '';

		$aliases = $this->getTableParams('alias');

		// If have the name of this field in params file
		if(isset($aliases[$value])) {
			$name = $aliases[$value];

		// If don't have parameter for this field and this is a primary
		} elseif(!empty($primary) && in_array($value, $primary)) {
			$name = 'ID';
		}
		
		if(empty($name)) {
			$values = explode('_', $value);
			if(count($values) > 1) {
				foreach($values as $part) {
					if(strlen($part) > 2) {
						$name .= ' '.$part;
					}
				}
			} else {
				$name = $value;
			}
		}
		
		return ucwords(trim($name));
	}
}